import 'package:cat_eye/config/color_config.dart';
import 'package:cat_eye/config/string_config.dart';
import 'package:cat_eye/provider/provider_widget.dart';
import 'package:cat_eye/root_page/find_page/find_page_widget.dart';
import 'package:cat_eye/root_page/home_page/home_page_widget.dart';
import 'package:cat_eye/root_page/hot_page/hot_page_widget.dart';
import 'package:cat_eye/root_page/my_attention_page/my_attention_widget.dart';
import 'package:cat_eye/root_page/my_page/my_page_widget.dart';
import 'package:cat_eye/util/permission_util.dart';
import 'package:cat_eye/util/util.dart';
import 'package:cat_eye/view_model/home_view_model/home_tab_view_model.dart';
import 'package:flutter/material.dart';

class RootPage extends StatefulWidget {
  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  //pageView 状态管理
  PageController pageController;

  @override
  void initState() {
    super.initState();
    pageController = PageController();

    //权限申请
    Future.delayed(Duration.zero, () {
      //权限申请
      var permissionUtil = PermissionUtil(context);
      permissionUtil.checkPermission();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        //禁止PageView 左右滑动
        physics: NeverScrollableScrollPhysics(),
        children: [
          //首页
          HomePageWidget(),

          //发现
          FindPageWidget(),

          //热门
          HotPageWidget(),

          //我的
          MyPageWidget(),
        ],
        //当打开页面显示第0个位置
        controller: pageController,
      ),
      bottomNavigationBar: ProviderWidget<HomeTabViewModel>(
        model: HomeTabViewModel(),
        builder: (context, model, child) {
          return BottomNavigationBar(
            currentIndex: model.currentIndex,
            selectedItemColor:themeColor(),
            unselectedItemColor: Color(0xff9a9a9a),
            onTap: (index) {
              //点击按钮使用PageView控制器控制当前页面
              pageController.jumpToPage(index);
              model.changeBottomTabIndex(index);
            },
            //将文本展示出来
            type: BottomNavigationBarType.fixed,
            items: [
              buildButtonItem(
                  Icon(Icons.home),
                  Icon(Icons.home,
                      color: themeColor()),
                  StringConfig.HOME),
              buildButtonItem(
                  Icon(Icons.find_in_page),
                  Icon(Icons.find_in_page,
                      color: themeColor()),
                  StringConfig.FIND),
              buildButtonItem(
                  Icon(Icons.hot_tub),
                  Icon(Icons.hot_tub,
                      color: themeColor()),
                  StringConfig.HOT),
              buildButtonItem(
                  Icon(Icons.android),
                  Icon(Icons.android,
                      color: themeColor()),
                  StringConfig.MY),
            ],
          );
        },
      ),
    );
  }

  /*
   * icon 选中图片
   *  activeIcon 未选中时候图片
   *  label 提示文字
   */
  BottomNavigationBarItem buildButtonItem(
      Icon icon, Icon activeIcon, String label) {
    return BottomNavigationBarItem(
      icon: icon,
      label: label,
      activeIcon: activeIcon,
    );
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }
}
